﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class user_uInfofb : System.Web.UI.Page
{
    string sql;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Session.Contents["wj_User"] == null)
            {
                Response.Redirect("../uPassport.aspx");
            }
            else
            {


                proinfo();
                bind_d();


                if (Request.QueryString["cp"] == "edit")　//显示文本信息
                {
                    pro_txt();
                    bind_t1();
                    bind_t2();
                    bind_t3();


                }
                else if (Request.QueryString["cp"] == "del")　//删除信息
                {
                    del_pro();
                }
            }
        }
    }

    #region
    public void proinfo()//判断信息
    {
        string se_user = Session.Contents["wj_User"].ToString();
        if (se_user != null)
        {
            DataTable dt = Class1.ExecSel("select * from wj_User where wj_User='" + se_user + "'");
            string wj_CompanyName = dt.Rows[0]["wj_CompanyName"].ToString();
            string C_One = dt.Rows[0]["C_One"].ToString();
            string C_Two = dt.Rows[0]["C_Two"].ToString();
            string C_Three = dt.Rows[0]["C_Three"].ToString();
            string AreaA = dt.Rows[0]["C_Three"].ToString();
            string AreaB = dt.Rows[0]["C_Three"].ToString();
            string AreaC = dt.Rows[0]["C_Three"].ToString();

            if (wj_CompanyName == "" || wj_CompanyName == null || C_One == "" || C_One == null || C_Two == "" || C_Two == null || C_Three == "" || C_Three == null || AreaA == "" || AreaA == null || AreaB == "" || AreaB == null || AreaC == "" || AreaC == null)
            {
                Class1.AlertShow("请先完善信息！", "uInformation.aspx");
            }
        }
        else
        {
            Response.Redirect("../uPassport.aspx");
        }
    }
    #endregion

    #region 绑定大类
    public void bind_d()//绑定大类
    {
        sql = "select * from Class where Menu_Level=1";
        DataTable dt = Class1.ExecSel(sql);
        this.DropDownList1.DataSource = dt;
        this.DropDownList1.DataTextField = "Menu_Name";
        this.DropDownList1.DataValueField = "Menu_ID";
        this.DropDownList1.DataBind();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ID = this.DropDownList1.SelectedValue;

        DropDownList2.Items.Clear();
        DropDownList2.Items.Add(new ListItem("全部", "0"));
        DropDownList3.Items.Clear();
        DropDownList3.Items.Add(new ListItem("全部", "0"));

        sql = "select * from Class where Menu_Level=2 and Menu_ParentLevel=" + ID;
        DataTable dt = Class1.ExecSel(sql);
        this.DropDownList2.DataSource = dt;
        this.DropDownList2.DataTextField = "Menu_Name";
        this.DropDownList2.DataValueField = "Menu_ID";
        this.DropDownList2.DataBind();

    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ID = this.DropDownList2.SelectedValue;

        DropDownList3.Items.Clear();
        DropDownList3.Items.Add(new ListItem("全部", "0"));

        sql = "select * from Class where Menu_Level=3 and Menu_ParentLevel=" + ID;
        DataTable dt = Class1.ExecSel(sql);
        DropDownList3.DataSource = dt;
        DropDownList3.DataValueField = "Menu_ID";
        DropDownList3.DataTextField = "Menu_Name";
        DropDownList3.DataBind();
    }
    #endregion

    #region 图片上传
    protected void UpImg_Click(object sender, EventArgs e)//上传图片
    {
        if (FileUpload1.PostedFile.ContentLength < 500000)//100000为100K
        {

            string fileFullname = this.FileUpload1.FileName;
            string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
            string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
            string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
            if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
            {
                this.FileUpload1.SaveAs(Server.MapPath("../Images") + "\\" + dataName + "." + type);
                string ProImg = "Images/" + dataName + "." + type;
                this.txtProImg.Text = "Images/" + dataName + "." + type;
            }
            else
            {
                Response.Write("<script language='javascript'>alert('支持格式：|jpg|gif|bmp|');</script>");
            }
        }
        else
        {
            Response.Write("<script language='javascript'>alert('你的图片已经超过500K的大小！');</script>");
        }
    }
    #endregion

    #region 添加
    public void pro_add()//执行添加
    {
        string Ddl1 = DropDownList1.SelectedValue;
        string Ddl2 = DropDownList2.SelectedValue;
        string Ddl3 = DropDownList3.SelectedValue;
        string Type = DropDownList4.SelectedItem.Text;
        string Title = TextBox1.Text;
        string ProImg = txtProImg.Text;
        string Amount = TextBox2.Text;
        string Packing = TextBox3.Text;
        string Prices = TextBox4.Text;
        string Specifications = TextBox5.Text;

        //string Period = txtEndTime.Text;
        int Period = Convert.ToInt32(DropDownList5.SelectedValue);

        //int a = int.Parse(txtEndTime.Text);//有效期
        //DateTime Period = DateTime.Now;
        //Period = Period.AddDays(a);


        string Content = ftbContent.Text.Trim();
        DateTime Time = DateTime.Now;

        if (Ddl1 == "0" || Ddl2 == "0" || Ddl3 == "0")
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "TestAlert", "alert('请选择分类！');", true);
            return;
        }

        else if (Title == "")
        {
            Response.Write("<script>alert('请填写标题！');history.go(-1);</script>");
            return;
        }
        else if (Period == 0)
        {
            Response.Write("<script>alert('请填写有效期！');history.go(-1);</script>");
            return;
        }
        else if (Type == "请选择")
        {
            Response.Write("<script>alert('请选择类型！');history.go(-1);</script>");
            return;
        }
        else
        {
            string se_user = Session.Contents["wj_User"].ToString();
            if (se_user != null)
            {
                sql = "select uFlg,wj_ID,wj_CompanyName,wj_Connecter,wj_Phone,wj_Mobile,wj_QQ,wj_Fax,wj_Email,wj_WebUrl,wj_Address,AreaA,AreaB,AreaC from wj_User where wj_User='" + se_user + "'";
                DataTable dt = Class1.ExecSel(sql);
                //if (dt.Rows[0]["uFlg"].ToString() == "0")
                //{
                //    p_Lock = "1";
                //}
                //else
                //{
                //    p_Lock = "0";
                //}
                string p_Lock = "1";
                string p_UserID = dt.Rows[0]["wj_ID"].ToString();
                string p_CompanyName = dt.Rows[0]["wj_CompanyName"].ToString();
                string Contacts = dt.Rows[0]["wj_Connecter"].ToString();
                string Tel = dt.Rows[0]["wj_Phone"].ToString();
                string Phone = dt.Rows[0]["wj_Mobile"].ToString();
                string Fax = dt.Rows[0]["wj_Fax"].ToString();
                string Email = dt.Rows[0]["wj_Email"].ToString();
                string Website = dt.Rows[0]["wj_WebUrl"].ToString();
                string Address = dt.Rows[0]["wj_Address"].ToString();
                string AreaA = dt.Rows[0]["AreaA"].ToString();
                string AreaB = dt.Rows[0]["AreaB"].ToString();
                string AreaC = dt.Rows[0]["AreaC"].ToString();
                string Connecter = dt.Rows[0]["wj_Connecter"].ToString();
                string QQ = dt.Rows[0]["wj_QQ"].ToString();
                sql = "insert into Product(p_UserID,p_CompanyName,p_b_ID,p_z_ID,p_s_ID,p_Type,p_Title,p_ProImg,p_Amount,p_Packing,p_Prices,p_Specifications,p_EndTime,p_Content,p_Contacts,p_Tel,p_Phone,p_Fax,p_Email,p_Website,p_Address,p_Time,p_Lock,p_AreaA,p_AreaB,p_AreaC,p_Connecter,p_QQ)";
                sql += "values(" + p_UserID + ",'" + p_CompanyName + "'," + Ddl1 + "," + Ddl2 + "," + Ddl3 + ",'" + Type + "','" + Title + "','" + ProImg + "'," + Amount + ",'" + Packing + "','" + Prices + "','" + Specifications + "','" + Period + "','" + Content + "','" + Contacts + "','" + Tel + "','" + Phone + "','" + Fax + "','" + Email + "','" + Website + "','" + Address + "','" + Time + "'," + p_Lock + ",'" + AreaA + "','" + AreaB + "','" + AreaC + "','" + Connecter + "','" + QQ + "')";
                Class1.ExecSql(sql);
                Class1.AlertShow("发布成功！", "uInfofb.aspx?cp=add");
            }
            else
            {
                Response.Redirect("../uPassport.aspx");
            }


        }

    }
    #endregion

    #region 显示文本信息
    public void pro_txt()//显示修改的文本信息
    {
        int id = int.Parse(Request.QueryString["ID"]);
        DataTable dt = Class1.ExecSel("select * from Product where p_ID='" + id + "'");
        TextBox1.Text = Convert.ToString(dt.Rows[0]["p_Title"]);
        txtProImg.Text = Convert.ToString(dt.Rows[0]["p_ProImg"]);
        TextBox2.Text = Convert.ToString(dt.Rows[0]["p_Amount"].ToString());
        TextBox3.Text = Convert.ToString(dt.Rows[0]["p_Packing"].ToString());
        TextBox4.Text = Convert.ToString(dt.Rows[0]["p_Prices"].ToString());
        TextBox5.Text = Convert.ToString(dt.Rows[0]["p_Specifications"].ToString());


        //DateTime dt1 = DateTime.Now;
        //DateTime dt2 = Convert.ToDateTime(dt.Rows[0]["p_EndTime"]);
        //System.TimeSpan st = dt2.Subtract(dt1);
        //Label1.Text = st.Days.ToString() + "天"
        //+ st.Hours.ToString() + "小时"
        //+ st.Minutes.ToString() + "分钟"
        //+ st.Seconds.ToString() + "秒";


        //txtEndTime.Text = Convert.ToString(dt.Rows[0]["p_EndTime"].ToString());

        ftbContent.Text = Convert.ToString(dt.Rows[0]["p_Content"].ToString());

        if (dt.Rows[0]["p_Type"].ToString().Trim() == "请选择")
        {
            this.DropDownList4.Items[0].Selected = true;
            this.DropDownList4.Items[1].Selected = false;
            this.DropDownList4.Items[2].Selected = false;
            this.DropDownList4.Items[3].Selected = false;
            this.DropDownList4.Items[4].Selected = false;
            this.DropDownList4.Items[5].Selected = false;
            this.DropDownList4.Items[6].Selected = false;
            this.DropDownList4.Items[7].Selected = false;
            this.DropDownList4.Items[8].Selected = false;
        }
        if (dt.Rows[0]["p_Type"].ToString().Trim() == "供应")
        {
            this.DropDownList4.Items[0].Selected = false;
            this.DropDownList4.Items[1].Selected = true;
            this.DropDownList4.Items[2].Selected = false;
            this.DropDownList4.Items[3].Selected = false;
            this.DropDownList4.Items[4].Selected = false;
            this.DropDownList4.Items[5].Selected = false;
            this.DropDownList4.Items[6].Selected = false;
            this.DropDownList4.Items[7].Selected = false;
            this.DropDownList4.Items[8].Selected = false;
        }
        if (dt.Rows[0]["p_Type"].ToString().Trim() == "采购")
        {
            this.DropDownList4.Items[0].Selected = false;
            this.DropDownList4.Items[1].Selected = false;
            this.DropDownList4.Items[2].Selected = true;
            this.DropDownList4.Items[3].Selected = false;
            this.DropDownList4.Items[4].Selected = false;
            this.DropDownList4.Items[5].Selected = false;
            this.DropDownList4.Items[6].Selected = false;
            this.DropDownList4.Items[7].Selected = false;
            this.DropDownList4.Items[8].Selected = false;
        }
        if (dt.Rows[0]["p_Type"].ToString().Trim() == "代理")
        {
            this.DropDownList4.Items[0].Selected = false;
            this.DropDownList4.Items[1].Selected = false;
            this.DropDownList4.Items[2].Selected = false;
            this.DropDownList4.Items[3].Selected = true;
            this.DropDownList4.Items[4].Selected = false;
            this.DropDownList4.Items[5].Selected = false;
            this.DropDownList4.Items[6].Selected = false;
            this.DropDownList4.Items[7].Selected = false;
            this.DropDownList4.Items[8].Selected = false;
        }
        if (dt.Rows[0]["p_Type"].ToString().Trim() == "合作")
        {
            this.DropDownList4.Items[0].Selected = false;
            this.DropDownList4.Items[1].Selected = false;
            this.DropDownList4.Items[2].Selected = false;
            this.DropDownList4.Items[3].Selected = false;
            this.DropDownList4.Items[4].Selected = true;
            this.DropDownList4.Items[5].Selected = false;
            this.DropDownList4.Items[6].Selected = false;
            this.DropDownList4.Items[7].Selected = false;
            this.DropDownList4.Items[8].Selected = false;
        }
        if (dt.Rows[0]["p_Type"].ToString().Trim() == "招投标")
        {
            this.DropDownList4.Items[0].Selected = false;
            this.DropDownList4.Items[1].Selected = false;
            this.DropDownList4.Items[2].Selected = false;
            this.DropDownList4.Items[3].Selected = false;
            this.DropDownList4.Items[4].Selected = false;
            this.DropDownList4.Items[5].Selected = true;
            this.DropDownList4.Items[6].Selected = false;
            this.DropDownList4.Items[7].Selected = false;
            this.DropDownList4.Items[8].Selected = false;
        }
        if (dt.Rows[0]["p_Type"].ToString().Trim() == "融资")
        {
            this.DropDownList4.Items[0].Selected = false;
            this.DropDownList4.Items[1].Selected = false;
            this.DropDownList4.Items[2].Selected = false;
            this.DropDownList4.Items[3].Selected = false;
            this.DropDownList4.Items[4].Selected = false;
            this.DropDownList4.Items[5].Selected = false;
            this.DropDownList4.Items[6].Selected = true;
            this.DropDownList4.Items[7].Selected = false;
            this.DropDownList4.Items[8].Selected = false;
        }
        if (dt.Rows[0]["p_Type"].ToString().Trim() == "厂店租售")
        {
            this.DropDownList4.Items[0].Selected = false;
            this.DropDownList4.Items[1].Selected = false;
            this.DropDownList4.Items[2].Selected = false;
            this.DropDownList4.Items[3].Selected = false;
            this.DropDownList4.Items[4].Selected = false;
            this.DropDownList4.Items[5].Selected = false;
            this.DropDownList4.Items[6].Selected = false;
            this.DropDownList4.Items[7].Selected = true;
            this.DropDownList4.Items[8].Selected = false;
        }
        if (dt.Rows[0]["p_Type"].ToString().Trim() == "其它")
        {
            this.DropDownList4.Items[0].Selected = false;
            this.DropDownList4.Items[1].Selected = false;
            this.DropDownList4.Items[2].Selected = false;
            this.DropDownList4.Items[3].Selected = false;
            this.DropDownList4.Items[4].Selected = false;
            this.DropDownList4.Items[5].Selected = false;
            this.DropDownList4.Items[6].Selected = false;
            this.DropDownList4.Items[7].Selected = false;
            this.DropDownList4.Items[8].Selected = true;
        }


        if (dt.Rows[0]["p_EndTime"].ToString().Trim() == "7")
        {
            this.DropDownList5.Items[0].Selected = false;
            this.DropDownList5.Items[1].Selected = true;
            this.DropDownList5.Items[2].Selected = false;
            this.DropDownList5.Items[3].Selected = false;
            this.DropDownList5.Items[4].Selected = false;
            this.DropDownList5.Items[5].Selected = false;
            this.DropDownList5.Items[6].Selected = false;
            this.DropDownList5.Items[7].Selected = false;
        }
        else if (dt.Rows[0]["p_EndTime"].ToString().Trim() == "15")
        {
            this.DropDownList5.Items[0].Selected = false;
            this.DropDownList5.Items[1].Selected = false;
            this.DropDownList5.Items[2].Selected = true;
            this.DropDownList5.Items[3].Selected = false;
            this.DropDownList5.Items[4].Selected = false;
            this.DropDownList5.Items[5].Selected = false;
            this.DropDownList5.Items[6].Selected = false;
            this.DropDownList5.Items[7].Selected = false;
        }
        else if (dt.Rows[0]["p_EndTime"].ToString().Trim() == "30")
        {
            this.DropDownList5.Items[0].Selected = false;
            this.DropDownList5.Items[1].Selected = false;
            this.DropDownList5.Items[2].Selected = false;
            this.DropDownList5.Items[3].Selected = true;
            this.DropDownList5.Items[4].Selected = false;
            this.DropDownList5.Items[5].Selected = false;
            this.DropDownList5.Items[6].Selected = false;
            this.DropDownList5.Items[7].Selected = false;
        }
        else if (dt.Rows[0]["p_EndTime"].ToString().Trim() == "60")
        {
            this.DropDownList5.Items[0].Selected = false;
            this.DropDownList5.Items[1].Selected = false;
            this.DropDownList5.Items[2].Selected = false;
            this.DropDownList5.Items[3].Selected = false;
            this.DropDownList5.Items[4].Selected = true;
            this.DropDownList5.Items[5].Selected = false;
            this.DropDownList5.Items[6].Selected = false;
            this.DropDownList5.Items[7].Selected = false;
        }
        else if (dt.Rows[0]["p_EndTime"].ToString().Trim() == "90")
        {
            this.DropDownList5.Items[0].Selected = false;
            this.DropDownList5.Items[1].Selected = false;
            this.DropDownList5.Items[2].Selected = false;
            this.DropDownList5.Items[3].Selected = false;
            this.DropDownList5.Items[4].Selected = false;
            this.DropDownList5.Items[5].Selected = true;
            this.DropDownList5.Items[6].Selected = false;
            this.DropDownList5.Items[7].Selected = false;
        }
        else if (dt.Rows[0]["p_EndTime"].ToString().Trim() == "180")
        {
            this.DropDownList5.Items[0].Selected = false;
            this.DropDownList5.Items[1].Selected = false;
            this.DropDownList5.Items[2].Selected = false;
            this.DropDownList5.Items[3].Selected = false;
            this.DropDownList5.Items[4].Selected = false;
            this.DropDownList5.Items[5].Selected = false;
            this.DropDownList5.Items[6].Selected = true;
            this.DropDownList5.Items[7].Selected = false;
        }
        else if (dt.Rows[0]["p_EndTime"].ToString().Trim() == "360")
        {
            this.DropDownList5.Items[0].Selected = false;
            this.DropDownList5.Items[1].Selected = false;
            this.DropDownList5.Items[2].Selected = false;
            this.DropDownList5.Items[3].Selected = false;
            this.DropDownList5.Items[4].Selected = false;
            this.DropDownList5.Items[5].Selected = false;
            this.DropDownList5.Items[6].Selected = false;
            this.DropDownList5.Items[7].Selected = true;
        }
    }
    #endregion

    #region 分类绑定
    public void bind_t1()//绑定大类
    {
        int id = int.Parse(Request.QueryString["ID"]);
        DataTable dt = Class1.ExecSel("select * from Product where p_ID=" + id + "");
        Session["dl"] = Convert.ToString(dt.Rows[0]["p_b_ID"].ToString());

        sql = "select * from Class where Menu_Level=1";
        DataTable dt2 = Class1.ExecSel(sql);
        this.DropDownList1.DataSource = dt2;
        this.DropDownList1.DataTextField = "Menu_Name";
        this.DropDownList1.DataValueField = "Menu_ID";
        this.DropDownList1.SelectedValue = Session["dl"].ToString();
        this.DropDownList1.DataBind();
    }
    public void bind_t2()//绑定中类
    {
        int id = int.Parse(Request.QueryString["ID"]);
        DataTable dt = Class1.ExecSel("select * from Product where p_ID=" + id + "");
        Session["zl"] = Convert.ToString(dt.Rows[0]["p_z_ID"].ToString());

        sql = "select * from Class where  Menu_ParentLevel=" + this.DropDownList1.SelectedValue;
        DataTable dt2 = Class1.ExecSel(sql);
        this.DropDownList2.DataSource = dt2;
        this.DropDownList2.DataTextField = "Menu_Name";
        this.DropDownList2.DataValueField = "Menu_ID";
        this.DropDownList2.SelectedValue = Session["zl"].ToString();
        this.DropDownList2.DataBind();
    }
    public void bind_t3()//绑定小类
    {
        int id = int.Parse(Request.QueryString["ID"]);
        DataTable dt = Class1.ExecSel("select * from Product where p_ID=" + id + "");
        Session["xl"] = Convert.ToString(dt.Rows[0]["p_s_ID"].ToString());

        sql = "select * from Class where Menu_ParentLevel=" + this.DropDownList2.SelectedValue;
        DataTable dt2 = Class1.ExecSel(sql);
        this.DropDownList3.DataSource = dt2;
        this.DropDownList3.DataTextField = "Menu_Name";
        this.DropDownList3.DataValueField = "Menu_ID";
        this.DropDownList3.SelectedValue = Session["xl"].ToString();
        this.DropDownList3.DataBind();
    }
    public void pro_edit()//修改信息
    {
        int id = int.Parse(Request.QueryString["ID"]);
        string p_Lock;
        string p_Home;
        string p_Helpful;
        string Ddl1 = DropDownList1.SelectedValue;
        string Ddl2 = DropDownList2.SelectedValue;
        string Ddl3 = DropDownList3.SelectedValue;
        string Type = DropDownList4.SelectedItem.Text;
        string Title = TextBox1.Text;
        string ProImg = this.txtProImg.Text;
        string Amount = TextBox2.Text;
        string Packing = TextBox3.Text;
        string Prices = TextBox4.Text;
        string Specifications = TextBox5.Text;
        //string Period = txtEndTime.Text;
        int Period = Convert.ToInt32(DropDownList5.SelectedValue);
        string Content = ftbContent.Text.Trim();


        if (Ddl1 == "0" && Ddl2 == "0" && Ddl3 == "0")
        {
            Response.Write("<script>alert('请选择分类！');history.go(-1);</script>");
            return;
        }
 
        else if (Period == 0)
        {
            Response.Write("<script>alert('请填写有效期！');history.go(-1);</script>");
            return;
        }
        else if (Title == "")
        {
            Response.Write("<script>alert('标题不能为空');history.go(-1);</script>");
            return;
        }
        else if (Title.Length > 100)
        {
            Response.Write("<script>alert('标题不能大于100个字！');history.go(-1);</script>");
            return;
        }
        else if (Type == "请选择")
        {
            Response.Write("<script>alert('请选择类型！');history.go(-1);</script>");
            return;
        }


        string sql = ("update Product set p_b_ID=" + Ddl1 + ",p_z_ID=" + Ddl2 + ",p_s_ID=" + Ddl3 + ",p_Type='" + Type + "',p_Title='" + Title + "',p_ProImg='" + ProImg + "',p_Amount='" + Amount + "',p_Packing='" + Packing + "',p_Prices='" + Prices + "',p_Specifications='" + Specifications + "',p_EndTime='" + Period + "',p_Content='" + Content + "' where p_ID=" + id + "");
        Class1.ExecSql(sql);
        Class1.AlertShow("修改成功！", "uInfoManage.aspx");

    }
    #endregion

    #region
    public void del_pro()//删除信息
    {
        int id = int.Parse(Request.QueryString["ID"]);
        string sql = "delete  Product where p_ID=" + id;
        Class1.ExecSql(sql);
        Class1.AlertShow("删除成功！", "uInfoManage.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["cp"] == "add")
        {
            pro_add();
        }
        else if (Request.QueryString["cp"] == "edit")
        {
            pro_edit();
        }
    }
    #endregion

    
}
